<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>countSubstrings</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const countSubstrings = (str, searchValue) => {
            let count = 0,
                i = 0;
            while (true) {
                const r = str.indexOf(searchValue, i);
                if (r !== -1) {
                    [count, i] = [count + 1, r + 1];
                } else {
                    return count;
                }
            }
        };
        console.log(countSubstrings('tiktok tok tok tik tok tik', 'tik')); // 3
        console.log(countSubstrings('tutut tut tut', 'tut')); // 4
    </script>
</body>

</html>